import 'package:flutter/material.dart';
import '../utils/app_theme.dart';
import 'settings_screen.dart';

class ProfileScreen extends StatelessWidget {
  const ProfileScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('个人中心'),
        actions: [
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () {
              Navigator.of(context).push(
                MaterialPageRoute(builder: (context) => const SettingsScreen()),
              );
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            // User profile card
            Container(
              padding: const EdgeInsets.all(20),
              decoration: AppTheme.cardDecoration,
              child: Column(
                children: [
                  const CircleAvatar(
                    radius: 40,
                    backgroundColor: AppTheme.primaryColor,
                    child: Icon(
                      Icons.person,
                      size: 40,
                      color: Colors.white,
                    ),
                  ),
                  const SizedBox(height: 16),
                  const Text(
                    '张三',
                    style: TextStyle(
                      fontSize: 20,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  const SizedBox(height: 4),
                  const Text(
                    'zhangsan@example.com',
                    style: TextStyle(
                      color: AppTheme.textSecondaryColor,
                    ),
                  ),
                  const SizedBox(height: 16),
                  OutlinedButton(
                    onPressed: () {
                      // Edit profile action
                    },
                    style: AppTheme.outlineButtonStyle,
                    child: const Text('编辑个人资料'),
                  ),
                ],
              ),
            ),
            const SizedBox(height: 20),
            
            // User stats
            Container(
              padding: const EdgeInsets.all(16),
              decoration: AppTheme.cardDecoration,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '我的数据',
                    style: AppTheme.subheadingStyle,
                  ),
                  const SizedBox(height: 16),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      _buildStatItem('总习惯', '5', Icons.list_alt),
                      _buildStatItem('已坚持', '32天', Icons.calendar_month),
                      _buildStatItem('完成率', '87%', Icons.bar_chart),
                    ],
                  ),
                ],
              ),
            ),
            const SizedBox(height: 20),
            
            // Achievements
            Container(
              padding: const EdgeInsets.all(16),
              decoration: AppTheme.cardDecoration,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '我的成就',
                    style: AppTheme.subheadingStyle,
                  ),
                  const SizedBox(height: 16),
                  _buildAchievement(
                    '习惯养成者',
                    '连续完成一个习惯7天',
                    Icons.emoji_events,
                    Colors.amber,
                    true,
                  ),
                  _buildAchievement(
                    '坚持不懈',
                    '连续完成一个习惯30天',
                    Icons.emoji_events,
                    Colors.amber,
                    false,
                  ),
                  _buildAchievement(
                    '全能达人',
                    '同时维持5个习惯',
                    Icons.emoji_events,
                    Colors.amber,
                    true,
                  ),
                  _buildAchievement(
                    '习惯大师',
                    '连续完成一个习惯365天',
                    Icons.emoji_events,
                    Colors.amber,
                    false,
                  ),
                ],
              ),
            ),
            const SizedBox(height: 20),
            
            // App info
            Container(
              padding: const EdgeInsets.all(16),
              decoration: AppTheme.cardDecoration,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                    '更多功能',
                    style: AppTheme.subheadingStyle,
                  ),
                  const SizedBox(height: 8),
                  _buildSettingsItem(
                    '邀请好友',
                    Icons.person_add,
                    () {
                      // Invite action
                    },
                  ),
                  _buildSettingsItem(
                    '给应用评分',
                    Icons.star,
                    () {
                      // Rate action
                    },
                  ),
                  _buildSettingsItem(
                    '关于应用',
                    Icons.info,
                    () {
                      _showAboutDialog(context);
                    },
                  ),
                  _buildSettingsItem(
                    '帮助与反馈',
                    Icons.help,
                    () {
                      // Help action
                    },
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
  
  Widget _buildStatItem(String label, String value, IconData icon) {
    return Column(
      children: [
        Icon(
          icon,
          size: 28,
          color: AppTheme.accentColor,
        ),
        const SizedBox(height: 8),
        Text(
          value,
          style: const TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.bold,
          ),
        ),
        Text(
          label,
          style: const TextStyle(
            color: AppTheme.textSecondaryColor,
            fontSize: 14,
          ),
        ),
      ],
    );
  }
  
  Widget _buildAchievement(
    String title,
    String description,
    IconData icon,
    Color color,
    bool unlocked,
  ) {
    return Opacity(
      opacity: unlocked ? 1.0 : 0.5,
      child: ListTile(
        leading: Container(
          padding: const EdgeInsets.all(8),
          decoration: BoxDecoration(
            color: color.withOpacity(0.2),
            shape: BoxShape.circle,
          ),
          child: Icon(icon, color: color),
        ),
        title: Text(
          title,
          style: TextStyle(
            fontWeight: unlocked ? FontWeight.bold : FontWeight.normal,
          ),
        ),
        subtitle: Text(description),
        trailing: unlocked
            ? const Icon(Icons.check_circle, color: AppTheme.successColor)
            : const Icon(Icons.lock, color: AppTheme.textSecondaryColor),
      ),
    );
  }
  
  Widget _buildSettingsItem(String title, IconData icon, VoidCallback onTap) {
    return ListTile(
      leading: Icon(icon, color: AppTheme.primaryColor),
      title: Text(title),
      trailing: const Icon(Icons.arrow_forward_ios, size: 16),
      onTap: onTap,
    );
  }
  
  void _showAboutDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (ctx) => AlertDialog(
        title: const Text('关于原子习惯'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            const SizedBox(height: 16),
            const Text(
              '原子习惯 App',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
              ),
            ),
            const SizedBox(height: 8),
            const Text('版本 1.0.0'),
            const SizedBox(height: 16),
            const Text(
              '基于詹姆斯·克利尔的《原子习惯》理念设计，帮助您培养和坚持良好的习惯。',
              textAlign: TextAlign.center,
            ),
            const SizedBox(height: 16),
            const Text(
              '© 2025 原子习惯团队',
              style: TextStyle(
                color: AppTheme.textSecondaryColor,
              ),
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.of(context).pop(),
            child: const Text('关闭'),
          ),
        ],
      ),
    );
  }
}
